package javacommon.filter;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;


/**

 * @author badqiu
 */
public class LoggerMDCFilter extends OncePerRequestFilter implements Filter {

	protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response, FilterChain chain)
			throws ServletException, IOException {
		try {
		/*	
			MDC.put("loginUserId", "demo-loginUsername");

			MDC.put("req.requestURI", StringUtils.defaultString(request
					.getRequestURI()));
			MDC.put("req.queryString", StringUtils.defaultString(request
					.getQueryString()));
			MDC.put("req.requestURIWithQueryString", request.getRequestURI()
					+ (request.getQueryString() == null ? "" : "?"
							+ request.getQueryString()));
			MDC.put("req.remoteAddr", StringUtils.defaultString(request
					.getRemoteAddr()));
*/
			
		//	System.out.println(JspFactory.getDefaultFactory().getPageContext(, request, response, "", false,8024, true));
			MDC.put("req.id", StringUtils.remove(UUID.randomUUID().toString(),"-"));
			String requestUri = request.getRequestURI();
			if(requestUri.indexOf(".")==-1) {
				
				/*
				 * String ticket= http://sso.ddmap.com/getTicket;
				 * ticket = dealTicket(ticket);
				 * http://sso.ddmap.com/checkTikect?ticket=
				 * ticket 
				 * 
				 */
				
			}
			clearMDC();
			chain.doFilter(request, response);
		} finally {
			
		}
	}

	private void clearMDC() {
		Map map = MDC.getContext();
		if (map != null) {
			map.clear();
		}
	}
}
